/**
 * 
 */

package com.e885.util.web;

import java.io.File;

import com.e885.util.UUIDHexGenerator;
import com.oreilly.servlet.multipart.FileRenamePolicy;

/**
 * 將上傳檔案檔名改為唯一值<br>
 * <br>
 * Copyright: Copyright (c) 2001-2008<br>
 * Company: E885 Group
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 */
public class UniqueFileRenamePolicy implements FileRenamePolicy
{

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.oreilly.servlet.multipart.FileRenamePolicy#rename(java.io.File)
	 */
	public File rename( File f )
	{
		String name = f.getName();

		String newName = null;
		String ext = null;

		int dot = name.lastIndexOf( "." );

		if( dot != -1 )
			ext = name.substring( dot ); // includes "."
		else
			ext = "";

		newName = new UUIDHexGenerator().generate() + ext;

		return new File( f.getParent(), newName );
	}
}
